﻿/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#ifndef __7FCDF8F7_3407_4528_AFC5_A15BDB01A655__
#define __7FCDF8F7_3407_4528_AFC5_A15BDB01A655__

#include "../tools/head.h"

/////////////////////////////////////////////////////////////////////////////////////////
// 

namespace xos
{
    // 
    // file_impl
    // 
    class file_impl : public xos_stl::mem_item< release_impl< file_impl >, thread_lock >,
        public i_file
    {
    public:
        typedef release_impl< file_impl > T;

    public:
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //

        static int get_addr( POOL *** pppPool, LIST *** pppList );

    public:
        file_impl();
        ~file_impl();

    public:
        // 
        // public for easily internal use
        // 
        enumFileType m_enumType;
        char m_szFileName[4096];
        int m_hFile;

    public:
        int init();
        int term();

    public:
        //////////////////////////////////////////////////////////////////////////////////
        // 
        // open a file
        // 
        int open( const char * lpszFileName, unsigned long ulAccessFlag, unsigned long ulShareMode, unsigned long ulCreateFlag );

        // 
        // get file time
        // 
        int get_file_time( xos::xos_i64 * lpCreateTime, xos::xos_i64 * lpAccessTime, xos::xos_i64 * lpModifyTime );

        // 
        // get file or dir name
        // 
        const char * get_file_name( char * lpszFileName, int nLen, int * pnLenNeeded );

        // 
        // is file or dir ? 
        // 
        enumFileType get_file_type();

        // 
        // move file pointer to ...
        // 
        int set_file_pos( enumFilePos enumFrom, xos_i64 iPos64, xos_i64 * lpSeek64 );

        // 
        // get current file pointer
        // 
        int get_file_pos( xos_i64 * lpSize64 );

        // 
        // get file size
        // 
        int get_file_size( xos_i64 * lpSize64 );

        // 
        // commit file disk space.
        // 
        int commit_file( xos_i64 iPos64 );

        // 
        // write file data
        // 
        // -1 means failed. other , write bytes
        // 
        int write( const void * lpMsg, unsigned long ulBufSize );

        // 
        // read file data
        // 
        // -1 means failed. other , read bytes
        //
        int read( void * lpMsg, unsigned long ulBufSize );

        // 
        // close file handle
        // 
        int close();
    };
} // xos

#endif // __7FCDF8F7_3407_4528_AFC5_A15BDB01A655__
